package mrpheu.demo.server.implement;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Level;
import java.util.logging.Logger;

import mrpheu.demo.basic.interfaces.IMrPheuIO;
import mrpheu.demo.basic.interfaces.IMrPheuOPC;
import mrpheu.demo.server.classes.MrPheuBasicServer;
import mrpheu.demo.server.classes.ThreadPoolChat;
import mrpheu.demo.server.interfaces.IZoneClient;


public class MrPheuTCPServer extends MrPheuBasicServer implements Runnable, IMrPheuOPC, IMrPheuIO {
    private Logger logger = Logger.getLogger(Thread.currentThread().getStackTrace()[1].getClassName());
    
    private ServerSocket server = null;
    
    public MrPheuTCPServer(int port, Map<Integer, IZoneClient> clientList, ThreadPoolChat threadPool) {
        super(port, clientList, threadPool);
    }
    @Override
    public boolean open() {
        this.logger.log(Level.WARNING, "TCPServer starting...");
        try {
            this.server = new ServerSocket(this.port);
            this.logger.log(Level.WARNING, this.server.toString());
        } catch (IOException e) {
            this.logger.log(Level.WARNING, "TCPServer's not start.");
            return false;
        }
        this.logger.log(Level.WARNING, "TCPServer started");
        return true;
    }
    @Override
    public void process() {
        if (this.server != null) {
            while (!super.offServer) {
                this.logger.log(Level.WARNING, "Waiting for client...");
                try {
                    Socket socket = this.server.accept();
                    this.logger.log(Level.WARNING, "Client " + socket + " connected.");
                    int id = ThreadLocalRandom.current().nextInt();
                    this.logger.log(Level.WARNING, "ID: " + id);
                    ProcessTCPClient clientThread = new ProcessTCPClient(id,
                            socket);
                    this.clientList.put(id, clientThread);
                    this.threadPool.execute(clientThread);
                } catch (IOException e1) {
                    this.logger.log(Level.WARNING, "Client fail.");
                }
            }
        }
    }
    @Override
    public boolean close() {
        if(this.server != null) {
            try {
                this.server.close();
            } catch (IOException e) {
                this.logger.log(Level.WARNING, "TCPServer close fail.");
                return false;
            }
        }
        return true;
    }
    @Override
    public void run() {
        this.logger.log(Level.WARNING, "Run....");
        this.open();
        this.process();
        this.close();
    }
    @Override
    public boolean sendData(Object data) {
        // TODO Auto-generated method stub
        return false;
    }
    @Override
    public Object receiveData() {
        // TODO Auto-generated method stub
        return null;
    }
    
    
    
}
